//
//  WLLEmoticonBottomView.swift
//  WLLWeibo
//
//  Created by 王乐乐 on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

//tag 参数
private let tagNumber = 250

enum WLLEmoticonBottomViewType : Int {
        ///  默认
    case Default = 0
    /// emoji
    case Emoji = 1
    /// 浪小花
    case Lxh = 2
}
//iOS 9.0 之后才适用-->UIStackView
//@available(iOS 9.0, *)
class WLLEmoticonBottomView: UIStackView {

    //当前选中的按钮
    var currentSelectedBtn : UIButton?
    
    //定义闭包
    var emoBottom_closure : ((type:WLLEmoticonBottomViewType)->())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 //MARK: - 设置视图
    func setupUI () {
        
        //填充方向
        axis = .Horizontal
        
        //填充方式distribution/分配
        distribution = .FillEqually
        
        //添加控件
        
        addChildButtons("默认", imgName: "left", type: WLLEmoticonBottomViewType.Default)
        addChildButtons("emoji", imgName: "mid", type: WLLEmoticonBottomViewType.Emoji)
        addChildButtons("浪小花", imgName: "right", type: WLLEmoticonBottomViewType.Lxh)
        
    }
    
     //MARK: - 供外界调用的方法...联动
    func setupCurrentButton(tag : Int) {
        
        //得到button
        let button = viewWithTag(tag + tagNumber) as! UIButton
        
        selectBtn(button)
        
    }
    
    
    
    //MARK: - 按钮点击事件
    func buttonClick(button:UIButton)  {
           printLog("底部按钮被点击了")
        
        selectBtn(button)
        
        //执行闭包
        emoBottom_closure?(type: WLLEmoticonBottomViewType(rawValue: button.tag - tagNumber)!)
        
    }
    // 选中设置判断
    func selectBtn (button:UIButton)  {
        if currentSelectedBtn == button {
            return
        }
        
        currentSelectedBtn?.selected = false
        
        button.selected = true
        
        currentSelectedBtn = button
        
     

    }
    
    
    func addChildButtons(title:String,imgName:String,type:WLLEmoticonBottomViewType) {
        
        let btn  = UIButton()
        btn.tag = type.rawValue + tagNumber
        //添加点击事件
        btn.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
     
        btn.setBackgroundImage(UIImage(named:"compose_emotion_table_\(imgName)_normal"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_selected"), forState: UIControlState.Selected)
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        if type == .Default {
            btn.selected = true
            currentSelectedBtn = btn
        }
//        addSubview(btn)
        addArrangedSubview(btn)

    }
    
    
    
    
    
    
}
